package com.devunion.bogdan.view.actions.annotations;

import com.devunion.bogdan.util.IconHelper;
import com.devunion.bogdan.view.editors.AnnotationManager;
import com.devunion.bogdan.view.editors.AnnotationView;
import com.devunion.bogdan.view.project.PageNode;
import com.devunion.bogdan.view.LibrarianEditor;
import com.devunion.bogdan.model.project.Page;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.util.List;

/**
 * @author Viktar Liaskovich
 */
public class RemoveAnnotationAction extends AbstractAction {

    public RemoveAnnotationAction() {
        super(null, IconHelper.getIcon("RemoveAnnotation16.gif"));
        putValue(Action.LARGE_ICON_KEY, IconHelper.getIcon("RemoveAnnotation24.gif"));
    }

    public void actionPerformed(ActionEvent e) {
        final JTree projectTree = LibrarianEditor.getInstance().getSelectedWorkspace().getProjectTreeBrowser().getProjectTree();

        Object lastPathComponent = projectTree.getSelectionPath().getLastPathComponent();

        if (lastPathComponent instanceof PageNode) {
            Page page = ((PageNode) lastPathComponent).getUserObject();

            List<Component> selectedComponents = AnnotationManager.getInstance().getSelectionManager().getSelectedComponents();

            for (Component c : selectedComponents) {
                if (c instanceof AnnotationView) {
                    page.removeAnnotation(((AnnotationView) c).getAnnotation());
                }
            }
        }

        for (Component toolbarButton : ((Component) e.getSource()).getParent().getComponents()) {
            if (toolbarButton instanceof JToggleButton) {
                ((JToggleButton) toolbarButton).setSelected(false); 
            }
        }
    }
}
